package top.callname.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.alibaba.fastjson.JSON;

import top.callname.bean.AdminBean;
import top.callname.bean.ClassBean;
import top.callname.bean.RspInfoBean;
import top.callname.bean.SignBean;
import top.callname.service.ClassService;
import top.callname.service.SignService;
import top.callname.service.impl.ClassServiceImpl;
import top.callname.service.impl.SignServiceImpl;
import top.callname.util.BaseUtil;
import top.callname.util.DateUtil;


@WebServlet("/admin/home")
public class HomeServlet extends HttpServlet {
	
	private static final long serialVersionUID = 1L;
  
	private SignService signService = new SignServiceImpl();
	private ClassService classService = new ClassServiceImpl();
	private Logger logger = Logger.getLogger(HomeServlet.class);

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String method = request.getParameter("method");
		response.setCharacterEncoding("UTF-8");
		if("stopSignTask".equals(method)){ 
			//结束签到任务
			stopSignTask(request,response);
		}else if("saveSignTask".equals(method)){
			//创建签到任务
			saveSignTask(request,response);
		}else if("infoSignTask".equals(method)){
			//查询任务详情
			infoSignTask(request,response);
		}else{
			//页面初始化
			initPage(request,response);
		}
		
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}
	
	
	private void infoSignTask(HttpServletRequest request,HttpServletResponse response) throws IOException{
	    PrintWriter out = response.getWriter();
		Integer signId = Integer.valueOf(request.getParameter("signId")); 
		SignBean signBean = signService.getSignTaskInfo(signId);
		logger.debug("查询签到任务详情："+signBean.toString());
		out.write(JSON.toJSONString(signBean));
	}
	
	private void saveSignTask(HttpServletRequest request,HttpServletResponse response) throws IOException{
	    PrintWriter out = response.getWriter();
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		SignBean signBean = new SignBean();
		String signDesc = request.getParameter("signDesc"); 
		String randNo =  request.getParameter("randNo"); 
		String timeOut = request.getParameter("timeOut");
		if(signDesc==null || "".equals(signDesc)){
			signDesc = admin.getFullname()+DateUtil.dateToStr(new Date());
		}
		String cid[] = request.getParameterValues("cids");
		String cids = "";
		for (int i = 0; i < cid.length; i++) {
			if(i != cid.length-1){
				cids+=cid[i]+",";
			}else{
				cids+=cid[i];
			}
		}
		signBean.setCids(cids);
		signBean.setRandNo(randNo);
		signBean.setPid(admin.getPid());
		signBean.setTimeOut(Integer.valueOf(timeOut));
		signBean.setDesc(signDesc);
		logger.debug("创建签到任务："+signBean.toString());
		RspInfoBean rsp = signService.addSignTask(signBean);
		out.write(JSON.toJSONString(rsp));
	}
	
	
	private void stopSignTask(HttpServletRequest request,HttpServletResponse response) throws IOException{
	    PrintWriter out = response.getWriter();
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		Integer signId = Integer.valueOf(request.getParameter("signId")); 
		RspInfoBean rsp = signService.stopSignTask(signId, admin.getPid());
		logger.debug("终止签到任务："+rsp.toString());
		out.write(JSON.toJSONString(rsp));
	}
	
	
	private void initPage(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException{
		
		AdminBean admin = (AdminBean) request.getSession().getAttribute("adminInfo");
		//查询班级列表
		List<ClassBean> list = classService.getList(admin.getPid());
		request.setAttribute("classList", list);
		
		//查询当前任务
		SignBean signBean = signService.getInuseSignByPid(admin.getPid());
		request.setAttribute("signInfo", signBean);
		
		//查询签到任务表
		String pageNo = request.getParameter("pageNo");
		int page = BaseUtil.getPageNo(pageNo);
		request.setAttribute("page", signService.getListPageByPid(page, Integer.valueOf(admin.getPid())) );
		request.getRequestDispatcher("home.jsp").forward(request,response);
	}

}
